package wxdb.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.*;

public class AddUser extends JDialog{
	private String[] databases = new String[Global.UI_REGISTER_DBMAX];
	private JList databaseList;
	
	private JButton addButton = new JButton("Add");
	private JButton clButton = new JButton("Cancel");
	private JTextField userName = new JTextField(10);
	private JPasswordField password = new JPasswordField(10);
	
	private JLabel addLabel = new JLabel("Add user :");
	private JLabel usrnameLabel = new JLabel("User Name :");
	private JLabel pwdLabel = new JLabel("Password :");
	
	private JPanel pane = new JPanel();
	
	private void setMyPane(){
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		pane.setLayout(gridbag);

		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(addLabel, constraints);
		pane.add(addLabel);
		
		constraints=new GridBagConstraints();
		constraints.anchor=GridBagConstraints.CENTER;
		constraints.insets=new Insets(5,5,5,5);
		gridbag.setConstraints(usrnameLabel, constraints);
		pane.add(usrnameLabel);
		
		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.fill=GridBagConstraints.HORIZONTAL;
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(userName, constraints);
		pane.add(userName);

		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.anchor=GridBagConstraints.CENTER;
		gridbag.setConstraints(pwdLabel, constraints);
		pane.add(pwdLabel);
		
		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.fill=GridBagConstraints.HORIZONTAL;
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(password, constraints);
		pane.add(password);	
		
		constraints=new GridBagConstraints();	
		constraints.insets=new Insets(5,5,5,5);
		JScrollPane registerPane = new JScrollPane(databaseList); 
		constraints.fill=GridBagConstraints.HORIZONTAL;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints(registerPane, constraints);
		pane.add(registerPane);
		
		constraints=new GridBagConstraints();	
		constraints.insets=new Insets(5,5,5,5);
		gridbag.setConstraints(addButton,constraints);
		pane.add(addButton);
		
		constraints=new GridBagConstraints();	
		constraints.insets=new Insets(5,5,5,5);
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(clButton,constraints);
		pane.add(clButton);
		
		setLocation(350,100);
		setContentPane(pane);
	}
	
	private void initListener(){
		ActListener actListener = new ActListener();
		addButton.addActionListener(actListener);
		clButton.addActionListener(actListener);
	}
	
	class ActListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == addButton)
				addButtonRun();
			if(e.getSource() == clButton)
				dispose();
		}
	}
	
	private void addButtonRun(){
		String usr = userName.getText();
		if(Global.isUserExist(usr)){
			JOptionPane.showMessageDialog(null,"User Existed , please try again","Error" ,JOptionPane.ERROR_MESSAGE);
			return;
		}
		String pwd = new String(password.getPassword());
		if(usr.equals("")){
			JOptionPane.showMessageDialog(null,"Inpute your username please","Error" ,JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(pwd.equals("")){
			JOptionPane.showMessageDialog(null,"Inpute your passwd please","Error" ,JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(databaseList.getSelectedValues().length==0){
			JOptionPane.showMessageDialog(null,"Select your databases please","Error" ,JOptionPane.ERROR_MESSAGE);
			return;
		}
		try{
			String fileName = "config/user.txt";
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			randomFile.seek(randomFile.length());
			String data = usr+";"+pwd+";";	
			String selectedDB[] = new String[databaseList.getSelectedValues().length];
			for(int i=0; i<databaseList.getSelectedValues().length ;i++){
				selectedDB[i] = (String) databaseList.getSelectedValues()[i];
				data = data + selectedDB[i] + ";";
			}
			data = data +"\r\n";
			randomFile.writeBytes(data);
			randomFile.close();
			Global.initUser();
			this.setVisible(false);
		}catch(Exception e){
			System.err.println(e.getMessage());
		};
	}
	
	public AddUser(){
		File file = new File("config/databases.txt");
		BufferedReader reader = null;
		int i=0;
		try{
			reader = new BufferedReader(new FileReader(file));
			String dbName = null;
			while((dbName = reader.readLine())!=null){
				databases[i] = new String(dbName);
				i++;
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		databaseList = new JList(databases);
		
		setTitle("Administrator-AddUser");
		setSize(300,450);
		initListener();
		setMyPane();
		setVisible(true);
		setResizable(false);		
	}
}
